/******************************************************************************\
    ReadWriteCriticalSection.hpp contains a class of critical section with 
	 different access rights.
    Copyright (C) 2008  Victor Vinogradov (fly3333@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
\******************************************************************************/

#ifndef __READ_WRITE_CRITICAL_SECTION__HPP
#define __READ_WRITE_CRITICAL_SECTION__HPP

#include "Semaphore.hpp"
#include "CriticalSection.hpp"

class cSubReadWriteCriticalSection : public cSubObject
{
public:
	cCriticalSection CriticalSection;
	cSemaphore ReadSemaphore, WriteSemaphore;
	int WaitingReaders, WaitingWriters, Active;

	cSubReadWriteCriticalSection()
		: WaitingReaders( 0 )
		, WaitingWriters( 0 )
		, Active( 0 )
	{
		if( !ReadSemaphore.Create( NULL, 0, MAXLONG, NULL ) )
			throw 0;
		if( !WriteSemaphore.Create( NULL, 0, MAXLONG, NULL ) )
			throw 0;
	}
	~cSubReadWriteCriticalSection()
	{
	}
};

class cReadWriteLockObject
{
public:
	virtual void Read() const = 0;
	virtual void Write() const = 0;
	virtual void Done() const = 0;
};

class cReadWriteCriticalSection : public cObject< cSubReadWriteCriticalSection >, public cReadWriteLockObject
{
public:
	bool Create()
	{
		cSubReadWriteCriticalSection *sub_read_write_critical_section;
		try
		{
			sub_read_write_critical_section = new cSubReadWriteCriticalSection();
		}
		catch( ... )
		{
			return false;
		}
		SwitchToSubObject( sub_read_write_critical_section );
		return true;
	}
	void Destroy()
	{
		SwitchToSubObject( NULL );
	}
	virtual void Read() const
	{
		if( GetSubObject() == NULL )
			return;
		bool is_write_pending;
		HANDLE semaphore;
		{
			cLocker locker( GetSubObject()->CriticalSection );
			is_write_pending = GetSubObject()->WaitingWriters > 0 || GetSubObject()->Active < 0;
			semaphore = GetSubObject()->ReadSemaphore;
			if( is_write_pending )
				++GetSubObject()->WaitingReaders;
			else
				++GetSubObject()->Active;
		}
		if( is_write_pending )
			WaitForSingleObject( semaphore, INFINITE );
	}
	virtual void Write() const
	{
		if( GetSubObject() == NULL )
			return;
		bool is_owned;
		HANDLE semaphore;
		{
			cLocker locker( GetSubObject()->CriticalSection );
			is_owned = GetSubObject()->Active != 0;
			semaphore = GetSubObject()->WriteSemaphore;
			if( is_owned )
				++GetSubObject()->WaitingWriters;
			else
				--GetSubObject()->Active;
		}
		if( is_owned )
			WaitForSingleObject( semaphore, INFINITE );
	}
	virtual void Done() const
	{
		if( GetSubObject() == NULL )
			return;
		cLocker locker( GetSubObject()->CriticalSection );
		if( GetSubObject()->Active == 0 )
			return;
		if( GetSubObject()->Active > 0 )
			--GetSubObject()->Active;
		else
			++GetSubObject()->Active;
		if( GetSubObject()->Active == 0 )
		{
			if( GetSubObject()->WaitingWriters > 0 )
			{
				--GetSubObject()->Active;
				--GetSubObject()->WaitingWriters;
				GetSubObject()->WriteSemaphore.Release( 1, NULL );
			}
			else if( GetSubObject()->WaitingReaders > 0 )
			{
				GetSubObject()->Active = GetSubObject()->WaitingReaders;
				GetSubObject()->WaitingReaders = 0;
				GetSubObject()->ReadSemaphore.Release( GetSubObject()->Active, NULL );
			}
			else
				;
		}
	}
};

class cReadLocker
{
	const cReadWriteLockObject &LockObject;
public:
	cReadLocker( const cReadWriteLockObject &lock_object )
		: LockObject( lock_object )
	{
		LockObject.Read();
	}
	~cReadLocker()
	{
		LockObject.Done();
	}
};

class cWriteLocker
{
	const cReadWriteLockObject &LockObject;
public:
	cWriteLocker( const cReadWriteLockObject &lock_object )
		: LockObject( lock_object )
	{
		LockObject.Write();
	}
	~cWriteLocker()
	{
		LockObject.Done();
	}
};

#endif
